//! Utilities for dispatching control signals from one tick to the next,
//! including scheduled block changes and messages.

use craftdig_math::BlkCoord;

use crate::block;

/// Data scheduled to be processed at a particular tick.
#[derive(Clone)]
pub struct RelayState {
    block_changes: Vec<BlockChange>,
}

impl RelayState {
    pub fn new(blocks: Vec<BlockChange>) -> Self {
        Self {
            block_changes: blocks,
        }
    }

    pub fn new_empty() -> Self {
        Self::new(Vec::new())
    }

    pub fn block_changes(&self) -> &[BlockChange] {
        &self.block_changes
    }

    pub fn unpack(self) -> Vec<BlockChange> {
        self.block_changes
    }
}

/// A block change on a particular tick, which triggers a few messages to be sent.
#[derive(Clone)]
pub struct BlockChange {
    target: BlkCoord,
    new_state: block::BlockStateSnapshot,
}

impl BlockChange {
    pub fn new(target: BlkCoord, new_state: block::BlockStateSnapshot) -> Self {
        Self { target, new_state }
    }

    pub fn target(&self) -> BlkCoord {
        self.target
    }

    pub fn new_state(&self) -> &block::BlockStateSnapshot {
        &self.new_state
    }
}
